﻿
@{
    ViewBag.Title = "指标管理";
}

<div class="row col-md-12 ml-0" style="min-height:5vh;">
    <strong class="font-rfs" style="margin-right: 2rem;padding-top: 0.4rem;">指标管理</strong>
    <select id="Spec-Select-FirstGroup" class="form-control font-rfs custom-headtoolbar-select" style="width:10%;"></select>
    <select id="Spec-Select-SecondGroup" class="form-control font-rfs custom-headtoolbar-select" style="width:10%;"></select>
    <button id="Spec-addSpec" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
    <button id="Spec-EditSpec" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
    <button id="Spec-DeleteSpec" class="btn btn-rfs"><i class="bi-trash" onclick=""></i> 删除</button>
</div>
<div class="row col-md-12 ml-0 pre-scrollable" style="min-height:90vh;">
    <div class="row col-md-12" style="padding-right:0px;left: 15px;padding-left: 0px;min-height:40vh;">
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12" style="min-height:40vh;">
            <table id="Spec-SpecList" class="table table-striped table-bordered table-sm table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>代码</th>
                        <th>名称</th>
                        <th>描述</th>
                        <th>一级分类</th>
                        <th>二级分类</th>
                        <th>-3σ</th>
                        <th>-2σ</th>
                        <th>标准值</th>
                        <th>+2σ</th>
                        <th>+3σ</th>
                        <th>验收标准</th>
                        <th>规则</th>
                        <th>版本</th>
                        <th>状态</th>
                        <th>启用</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
    <div class="row col-md-12 ml-0" style="margin-top: 1vh; padding-right:0px;padding-left: 0px;min-height:49vh;">
        <div class="row col-md-12 tab-content" style="padding-left: 0;padding-right: 0px;right: -15px;min-height:3vh;">
            <ul class="nav col-md-12" style="padding-right: 0;margin-bottom: 1px;">
                <li class="tabButton col-md-1 tab-first-active font-rfs" style="text-align: center;"><a href="#tabAnalyte" data-toggle="tab" class="text-decoration-none">分析项</a></li>
                <li class="col-md-11 tab-Notactive" style="text-align: center;"><a href="" data-toggle="tab"></a></li>
            </ul>
        </div>
        <div class="col-md-12 ml-0 mr-0 tab-pane active tab-content tab-body" id="tabAnalyte" style="padding-right: 0px;padding-left: 0px;top: -2px;min-height:46vh;">
            <div class="row col-md-12" style="min-height:2vh;">
                <button id="Spec-tabAnalyte-addAnalyte" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
                <button id="Spec-tabAnalyte-delAnalyte" class="btn btn-rfs"><i class="bi-trash" onclick=""></i> 删除</button>
            </div>
            <div class="table-responsive-sm p-0 bg-white rounded col-md-12" style="min-height:41vh;">
                <table id="Spec-Analyte" class="table table-striped table-bordered table-sm table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>代码</th>
                            <th>名称</th>
                            <th>分类</th>
                            <th>描述</th>
                            <th>结果模式</th>
                            <th>修约</th>
                            <th>单位</th>
                            <th>默认指标名</th>
                            <th>报告过滤</th>
                            <th>发布状态</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="SAMPLE-Spec-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="modalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div id="Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="DeleteConfirm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前选中记录吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="modal-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="Spec-DelSpecRelation-modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前指标与分析项的关联关系吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="Spec-DelSpecRelation-modal-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="Spec-DelSpecRelation-modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var Spec_SpecId = "";
        var Spec_AnalyteId = "";

        /*页面加载时，加载 一级分类 信息*/
        $.post("GetSpecFirstGroup", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#Spec-Select-FirstGroup").append(new Option(data[i].firstGroup));
            }
            $("#Spec-Select-FirstGroup").prepend(new Option("全部"));
            $("#Spec-Select-FirstGroup").prepend(new Option("", ""));
            $("#Spec-Select-FirstGroup").val("");
        });

        $("#Spec-Select-FirstGroup").change(function () {
            $.post("GetSpecSecondGroup", { /* 加载 二级分类 信息*/
                FirstGroup: $("#Spec-Select-FirstGroup").find("option:selected").text()  // 传递的参数，对应控制器里的参数
            }, function (data) {
                $("#Spec-Select-SecondGroup").empty();
                for (var i = 0; i < data.length; i++) {
                    $("#Spec-Select-SecondGroup").append(new Option(data[i].secondGroup));
                }
                $("#Spec-Select-SecondGroup").prepend(new Option("", ""));
                $("#Spec-Select-SecondGroup").val("");

                RefreshSpecTable();
            });
        });

        $("#Spec-Select-SecondGroup").change(function () {
            RefreshSpecTable();
        });

        function SetAcceptanceRulesButten(value, row, index) {
            var strHtml = "<button class='table-cell-button btn-rfs' data-SpecId=" + row.id + "  title='自定义计算规则' onclick='AcceptanceRulesButtenClick(this)'><i class='bi-file-code'></i></button>";
            return strHtml;
        }

        function AcceptanceRulesButtenClick(obj) {
            $("#modalTitle").text("验收规则");

            $("#SAMPLE-Spec-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("SpecAcceptanceRules", {
                Id: $(obj.outerHTML).attr("data-SpecId"),
                ModalID: "SAMPLE-Spec-Modal"
            },  function (data) {
                $("#Embed").html(data);
            });
        }

        function SpecEnableOnClick(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).attr("data-rowid");

            $.post("UpdateSpecEnable", {
                Id: rowid,
                Enable: st
            },  function (data) {
                alert(data);
            });
        }

        function UpdateAcceptanceCriteria(obj) {
            var st = $(obj).val();
            $.post("postUpdateSpecAcceptanceCriteria", {
                Id: Spec_SpecId,
                AcceptanceCriteria: st
            },  function (data) {
                alert(data);
            });
        }

        function SetSpecEnableCortrol(value, row, index){
            var strHtml = "";
            if(row.enable) {
                strHtml = "<input type='checkbox' checked onclick='SpecEnableOnClick(this)' data-rowid='" + row.id + "'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='SpecEnableOnClick(this)' data-rowid='" + row.id + "'/>";
            }
                               
            return strHtml;
        }

        function SetSpecReleaseStatus(value, row, index) {
            var strHtml = "";
            if(row.releaseStatus) {
                strHtml = "<i class='bi bi-info-circle-fill biInfoTrue'></i>";
            } else {
                strHtml = "<i class='bi bi-info-circle-fill biInfoFalse'></i>";
            }
                               
            return strHtml;
        }

        function SetAcceptanceCriteriaText(value, row, index) {
            var text = value == null ? '-' : value;
            var strHtml = "<input class='table-cell-input' onchange='UpdateAcceptanceCriteria(this)' value='" + text + "'/>";                               
            return strHtml;
        }

        function RefreshSpecTable() {
            Spec_SpecId = "";  /*清空该值*/
            $('#Spec-SpecList').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            var columns = [{ field: "id", visible: false }, { field: "code" }, { field: "name" },
            { field: "description" }, { field: "firstGroup" }, { field: "secondGroup" }, { field: "lowA", width: "5%" },
            { field: "lowB", width: "5%" }, { field: "standardValue", width: "5%" }, { field: "highB", width: "5%" }, { field: "highA", width: "5%" },
            { field: "acceptanceCriteria", formatter: SetAcceptanceCriteriaText },
            { field: "acceptanceRules", width:"5%", formatter: SetAcceptanceRulesButten }, { field: "version", width:"5%", align:"center" },
            { field: "releaseStatus", width: "5%", align: "center"/*,formatter: SetSpecReleaseStatus 暂时屏蔽状态图标，只显示文本*/ },
            { field: "enable", width:"5%", align:"center", formatter: SetSpecEnableCortrol }];

            $('#Spec-SpecList').bootstrapTable({
                method: 'post',
                url: "GetSpecListByFirstAndSecondAllField",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit,
                        offset: params.offset,

                        FirstGroup: $("#Spec-Select-FirstGroup").find("option:selected").text(),  // 传递的参数，这的名称要和控制器里对应的参数名称一样
                        SecondGroup: $("#Spec-Select-SecondGroup").find("option:selected").text()
                    };
                    return data;
                }, //参数
                queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageSize: 10,   // 默认页大小
                pageList: [10, 15, 20, 30],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
                onClickRow: function (row, $element, field) {
                    $("#Spec-SpecList").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    Spec_SpecId = row.id;
                    LoadAnalyte(Spec_SpecId);
                },
                silent: true,  //刷新事件必须设置
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#Spec-SpecList').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }
        
        function LoadAnalyte(SpecId) { 
            Spec_AnalyteId = "";
            var columns = [{ field: "id", visible: false }, { field: "code" }, { field: "name" }, { field: "classification" }, { field: "description"}, 
            { field: "resultModel" },{ field: "roundingOff" }, { field: "units" }, { field: "defaultSpecName" }, { field: "assist" }, { field: "releaseStatus" }];

            $('#Spec-Analyte').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#Spec-Analyte').bootstrapTable({
                method: 'post',
                url: "GetSpecRelationAnalytes",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        SpecId: SpecId,  // 传递的参数，这的名称要和控制器里对应的参数名称一样
                    };
                    return data;
                }, //参数
                queryParamsType: "search", //参数格式,发送标准的RESTFul类型的参数请求

                uniqueId: "id",          // 设置主键
                //search: true,  // 显示查询工具栏
                //showRefresh: true,  // 显示刷新按钮
                //showColumns: true,  // 可控制列的隐藏和显示
                //pagination: true,  // 显示分页条,设为true，启用了分页功能  暂时不分页
                //pageSize: 15,   // 默认页大小
                //pageList: [5, 10, 15, 20],  // 控制每页数目
                //showToggle: true,  //
                locale: "zh-CN",
                columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
                onClickRow: function (row, $element, field) {
                    $("#Spec-Analyte").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    Spec_AnalyteId = row.id;
                },
                silent: true,  //刷新事件必须设置
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#Analyte').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
            //alert(SpecId);
        }

        $("#Spec-DeleteSpec").click(function () {
            if (Spec_SpecId == "") {
                return false;
            }

            $("#DeleteConfirm").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#modal-delete").click(function () {
            $.post("postDeleteSpec", {
                Id: Spec_SpecId
            }, function (data) {
                if (data == "删除成功！") {
                    $('#Spec-SpecList').bootstrapTable('removeByUniqueId', Spec_SpecId);  // 删除指定的行
                    Spec_SpecId = "";  // 删除行后，将这个值置空，否则直接编辑信息就会报错。
                } else {
                    alert(data);
                }
            });

            $("#DeleteConfirm").modal('hide');
        });

        $("#Spec-DelSpecRelation-modal-delete").click(function () {
            $.post("DelteRelationSpecAndAanylte", {
                AnalyteID: Spec_AnalyteId
            }, function (data) {
                if (data == "删除成功") {
                    $('#Spec-Analyte').bootstrapTable('removeByUniqueId', Spec_AnalyteId);  // 删除指定的行
                    Spec_AnalyteId = "";  // 删除行后，将这个值置空，否则直接编辑信息就会报错。
                } else {
                    alert(data);
                }
            });

            $("#Spec-DelSpecRelation-modal").modal('hide');
        });

        $("#Spec-addSpec").click(function () {
            $("#modalTitle").text("添加指标");

            $("#SAMPLE-Spec-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddSpec", {
                Id:"",
                ModalID: "SAMPLE-Spec-Modal"
            }, function (datas) {
                $("#Embed").html(datas);
            });
        });

        $("#Spec-EditSpec").click(function () {
            if (Spec_SpecId.length == 0) {
                alert("请先选择需要编辑的指标！");
                return;
            }

            $("#modalTitle").text("编辑指标");

            $("#SAMPLE-Spec-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#Spec-EditSpec", parent.document.body).attr("src", "AddSpec");
            $.post("AddSpec", {
                id: Spec_SpecId,
                ModalID: "SAMPLE-Spec-Modal"
            }, function (datas) {
                $("#Embed").html(datas);
            });
        });

        $('#SAMPLE-Spec-Modal').on('hidden.bs.modal', function (event) {
            if (event.target == this) {
                if (Spec_SpecId.length > 0) {
                    $.post("GetSpec", {
                        Id: Spec_SpecId
                    }, function (data) {
                        var row = $('#Spec-SpecList').bootstrapTable('getRowByUniqueId', Spec_SpecId);
                        row.code = data.code;
                        row.name = data.name;
                        row.description = data.description;
                        row.lowA = data.lowA;
                        row.lowB = data.lowB;
                        row.standardValue = data.standardValue;
                        row.highA = data.highA;
                        row.highB = data.highB;
                        row.acceptanceCriteria = data.acceptanceCriteria;
                        row.acceptanceText = data.acceptanceText;
                        $('#Spec-SpecList').bootstrapTable('updateByUniqueId',row);
                        LoadAnalyte(Spec_SpecId);
                    });
                }
            }
        });

        $("#Spec-tabAnalyte-addAnalyte").click(function () {
            if (Spec_SpecId.length == 0) {
                alert("请先选择需要添加关系的指标！");
                return;
            }

            $("#modalTitle").text("添加关联分析项");

            $("#SAMPLE-Spec-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
            
            $.post("AddSpecRelationAnalyte", {
                Id :Spec_SpecId,
                ModalID: "SAMPLE-Spec-Modal"
            }, function (datas) {
                $("#Embed").html(datas);
            });
        });

        $("#Spec-tabAnalyte-delAnalyte").click(function () {
            if (Spec_AnalyteId.length == 0) {
                alert("请先选择需要删除关系的分析项！");
                return;
            }

            $("#modalTitle").text("删除关联分析项");

            $("#Spec-DelSpecRelation-modal").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $(".tabButton").hover(
            function () {
                $('.tabButton').addClass('tab-first-mouse'); // 鼠标进入时绑定样式
            },
            function () {
                $('.tabButton').removeClass('tab-first-mouse'); // 鼠标进入时绑定样式
        });

    </script>
}
